package com.gitee.huanminabc.jcommon.test;


import sun.misc.Launcher;

import java.io.*;
import java.nio.file.Paths;

/**
 * 各种路径获取
 * @Author: huanmin
 * @Date: 2019/1/4
 */
public class PathUtil {

    /**
     * 获取资源文件流, 项目打包后也能拿到jar包里的文件, 而通过路径是拿不到的
     * @param path    路径是依据资源目录来获取的  /xxx  或者 xxx
     * @return
     * @throws IOException
     */
    public static InputStream getResourceFileStream(String path) throws IOException {
        path = cutFirstSeparatorAdaptation(path);
        return PathUtil.class.getClassLoader().getResourceAsStream(path);
    }


    /**
     * 获取当前项目的绝对路径 ,F:\\java-project\\java-huanmin-utils\\utils-test
     * 打包后获取的是jar包的绝对路径，不包括jar的名称，也就是和jar在同一级
     * @return
     */
    public static String getCurrentProjectAbsolutePath() {
        return System.getProperty("user.dir");
    }


    /**
     * 获取当前项目resources的绝对路径 ,F:\\java-project\\java-huanmin-utils\\utils-test\\src\\main\\resources
     * 只用于IDEA测试
     * @return
     */
    public static String getCurrentProjectResourcesAbsolutePath() {
        String userDir = System.getProperty("user.dir");
        return userDir + File.separator + "src" + File.separator + "main" + File.separator + "resources";
    }


    /**
     * 获取当前项目resources下的文件的绝对路径 ,F:\\java-project\\java-huanmin-utils\\utils-test\\src\\main\\resources\\database.properties
     * 只用于IDEA测试, 项目打包后, 这种方式就不能用了,因为打包后资源是在压缩包里面了，通过路径是拿不到的
     * @param fileName
     * @return
     */

    public static String getCurrentProjectResourcesAbsolutePath(String fileName) {
        String userDir = System.getProperty("user.dir");
        fileName = cutSeparatorAdaptation(fileName);
        String s = userDir + File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator + fileName;
        return Paths.get(s).toString();
    }





    /**
     * 只用于IDEA测试, 项目打包后, 这种方式就不能用了,因为打包后资源是在压缩包里面了，通过路径是拿不到的
     * @param fileName
     * @return
     */
    public static File getCurrentProjectResourcesAbsoluteFile(String fileName) {
        String currentProjectResourcesFileAbsolutePath = getCurrentProjectResourcesAbsolutePath(fileName);
        return new File(currentProjectResourcesFileAbsolutePath);
    }
    //获取测试目录下的Resources的绝对路径 ,F:\\java-project\\java-huanmin-utils\\utils-test\\src\\test\\resources
    //只用于IDEA测试, 项目打包后, 这种方式就不能用了

    public static String getCurrentProjectTestResourcesAbsolutePath(String fileName) {
        String userDir = System.getProperty("user.dir");
        return userDir + File.separator + "src" + File.separator + "test" + File.separator + "resources" + File.separator + fileName;
    }
    public static File getCurrentProjectTestResourcesAbsoluteFile(String fileName) {
        String currentProjectTestResourcesFileAbsolutePath = getCurrentProjectTestResourcesAbsolutePath(fileName);
        return new File(currentProjectTestResourcesFileAbsolutePath);
    }



    //获取classpath下的文件的绝对路径 ,F:\\java-project\\java-huanmin-utils\\utils-test\\target\\classes\\abc\\database.properties
    // 只用于IDEA测试, 项目打包后, 这种方式就不能用了
    public   static  String getCurrentProjectTargetClassAbsolutePath(String filePath) {
        String userDir = System.getProperty("user.dir");
        filePath = cutSeparatorAdaptation(filePath);
        String s = userDir + File.separator + "target" + File.separator + "classes" + File.separator + filePath;
        return Paths.get(s).toString();
    }
    //获取测试的test-classes下的文件的绝对路径 ,F:\\java-project\\java-huanmin-utils\\utils-test\\target\\test-classes\\abc\\database.properties
    // 只用于IDEA测试, 项目打包后, 这种方式就不能用了
    public   static  String getCurrentProjectTargetTestClassAbsolutePath(String filePath) {
        String userDir = System.getProperty("user.dir");
        filePath = cutSeparatorAdaptation(filePath);
        String s = userDir + File.separator + "target" + File.separator + "test-classes" + File.separator + filePath;
        return Paths.get(s).toString();
    }


    //获取target目录
    //只用于IDEA测试, 项目打包后, 这种方式就不能用了
    public static String getCurrentProjectTargetAbsolutePath() {
        String path = Launcher.getLauncher().getClassLoader().getResource("").getPath();
        //截取target目录
        return path.substring(0, path.indexOf("target") + 6).substring(1);
    }




    private  static  String cutSeparatorAdaptation(String relativePath){
        relativePath = cutFirstSeparatorAdaptation(relativePath);
        relativePath = cutLastSeparatorAdaptation(relativePath);
        return relativePath;
    }

    /**
     * 去掉首个分割符,然后适配当前系统的分割符
     *
     * @param relativePath
     * @return
     */
    private static String cutFirstSeparatorAdaptation(String relativePath) {
        char c = relativePath.charAt(0);
        if (c == '/' || c == '\\') {//判断首个是否是分割符
            relativePath= relativePath.substring(1);
            return cutFirstSeparatorAdaptation(relativePath);//递归
        }
        //适配当前系统的分割符
        relativePath = relativePath.replace("\\*|/*", File.separator);
        return relativePath;
    }

    /**
     * 去掉结尾分割符,然后适配当前系统的分割符
     *
     * @param relativePath
     * @return
     */
    private static String cutLastSeparatorAdaptation(String relativePath) {
        //判断结尾是否是分割符
        char c = relativePath.charAt(relativePath.length() - 1);
        if (c == '/' || c == '\\') {//判断结尾是否是分割符
            relativePath = relativePath.substring(0, relativePath.length() - 1); //去掉第一个/
            return cutLastSeparatorAdaptation(relativePath);//递归
        }
        //适配当前系统的分割符
        relativePath = relativePath.replace("\\*|/*", File.separator);
        return relativePath;
    }

}
